var app = angular.module('ZJPC');

app.controller('FaceController', ['$scope', '$filter', '$state','$uibModalInstance', '$http', 'DataService', 'data', 'CesiumService','ModalService', 
  function($scope, $filter, $state, $uibModalInstance, $http, DataService, data, CesiumService,ModalService) {

    $scope.cancel = function() {
      $uibModalInstance.dismiss('cancel');
    };

    $scope.loading = true;
    var getFaceTree = DataService.deepCopy(getTree);

    $scope.treeData = getFaceTree.renlian;
    if(data instanceof Array == false){
      if(data.checked){
        var num = data.checked.substr(3);
      }else{
        var num = data.substr(3);        
      }
    }else{
      var num = [];
      for(var i = 0; i < data.length; i++){
        num.push(substr(data[i]));
      }
    }

    $scope.showList = true;
    var checkType = 1;
    $scope.checkShow = function (value) {
      if(value == 1) {
        $scope.showList = true;
        checkType = 1;
        $scope.pageOptions.current = 0;
        getData(newBm, $scope.starttime, $scope.endtime, $scope.pageOptions.current, $scope.pageOptions.pageSize, 1);
      }else {
        $scope.showList = false;
        checkType = 0;
        $scope.pageOption.current = 0;
        getData(newBm, $scope.starttime, $scope.endtime, $scope.pageOption.current, $scope.pageOption.pageSize, 0);
      }
    }
    
    $scope.treeData[setStatus(num)];
    $scope.treeConfig = {
          core : {
              multiple : true,
              animation: true,
              error : function(error) {
                  $log.error('treeCtrl: error from js tree - ' + angular.toJson(error));
              },
              check_callback : true,
              worker : true
          },
          types : {
              default : {
                  icon : 'glyphicon glyphicon-flash'
              },
              star : {
                  icon : '/img/gather.png'
              },
              cloud : {
                  icon : '/img/face_ico.png'
              }
          },
          version : 1,
          plugins : ['types','checkbox']
    };


    //点击不同点位树形图的展示
    function setStatus (id){
      if(id == '#'){
        return;
      }
      for(var i = 0; i < $scope.treeData.length; i ++){
        if($scope.treeData[i].id == id){
          $scope.treeData[i].state.opened = true;
          if($scope.treeData[i].type == 'cloud'){
            $scope.treeData[i].state.selected = true;
          }
          setStatus($scope.treeData[i].parent);
          return;
        }
      }
    }

    $scope.pageOptions = {
        pageSize:9,
        itemCount:0,
        current:0
    }

    $scope.pageOption = {
        pageSize:18,
        itemCount:0,
        current:0
    }

    $scope.endtime = new Date();
    $scope.starttime = new Date('2018/01/04');
    $scope.starttime.setHours(0,0,0);
    $scope.endtime.setHours(23,59,59);
    getData(num, $scope.starttime, $scope.endtime, $scope.pageOptions.current, $scope.pageOptions.pageSize, 1);

    var bm = [];
    var cm = [];
    //选中tree中元素触发
    $scope.readyCB = function (e, m){
      if(m.node.children.length > 0){
        for(var i = 0;i < m.node.children.length; i++){
          bm.push(m.node.children[i]);
        }
      }else{
        bm = m.selected;
      }
      cm = [];
      for (var i = 0; i < bm.length; i++) {
        if(bm[i].substr(0, 5) != 'ajson'){
          cm.push(bm[i]);
        }
      }
    }

    //取消选中tree中元素
    $scope.uncheck = function (e, m){
      if(m.node.children.length > 0){
        for(var i = 0;i < m.node.children.length; i++){
          bm.push(m.node.children[i]);
        }
      }else{
        bm = m.selected;
      }
      cm = [];
      for (var i = 0; i < bm.length; i++) {
        if(bm[i].substr(0, 5) != 'ajson'){
          cm.push(bm[i]);
        }
      }
    }

    var newBm = num;
    $scope.searchFace = function (){
      $scope.loading = true;
      newBm = cm;
      if(checkType == 1){
        getData(newBm, $scope.starttime, $scope.endtime, $scope.pageOptions.current, $scope.pageOptions.pageSize, 1);
      }else{
        getData(newBm, $scope.starttime, $scope.endtime, $scope.pageOption.current, $scope.pageOption.pageSize, 0);
      }
    }

    $scope.getContrast = function(item){
      getData(newBm, $scope.starttime, $scope.endtime, $scope.pageOptions.current, $scope.pageOptions.pageSize, 1);
    }

    $scope.getData = function(item){
      getData(newBm, $scope.starttime, $scope.endtime, $scope.pageOption.current, $scope.pageOption.pageSize, 0);
    }

    //type 0 校外人员  1 校内人员
    function getData (param, starttime, endtime, page, pageNum, type){
      var starttime = $filter('date')(starttime, 'yyyy-MM-dd HH:mm:ss');
      var endtime = $filter('date')(endtime, 'yyyy-MM-dd HH:mm:ss');
      DataService.restGetApi('school/track/getFace?jid=&&list=' + param + '&&starttime=' + starttime + '&&endtime=' + endtime + '&&page=' + page + '&&pageNum=' + pageNum + '&&type=' + type).then(function (face_data){
        $scope.loading = false;
        $scope.faceData = face_data.data.data;
        if(type==0){
          $scope.pageOption.itemCount = face_data.data.total;
        }else{
          $scope.pageOptions.itemCount = face_data.data.total;
        }

      },function (err){
        console.log(err);
      });
    }


    $scope.openIt = function(data,type){
      if(type=='facedetail'){
         ModalService.openModalBySize('../templates/region/detail/facedetail.html', 'FaceDetailController', data,'minisize')
      }else{
        ModalService.openModalBySize('../templates/region/detail/facesingledetail.html', 'FaceSingleDetailController', data,'facesize')
      }
    }

    
    /*时间控件校验相关设置*/
    $scope.endDateBeforeRender = endDateBeforeRender
    $scope.endDateOnSetTime = endDateOnSetTime
    $scope.startDateBeforeRender = startDateBeforeRender
    $scope.startDateOnSetTime = startDateOnSetTime

    function startDateOnSetTime () {
      $scope.$broadcast('start-date-changed');
    }

    function endDateOnSetTime () {
      $scope.$broadcast('end-date-changed');
    }

    function startDateBeforeRender ($dates) {
      if ($scope.endtime) {
        $scope.endtime = new Date($scope.endtime).setHours(23,59,59)
        var activeDate = moment($scope.endtime);

        $dates.filter(function (date) {
          return date.localDateValue() >= activeDate.valueOf()
        }).forEach(function (date) {
          date.selectable = false;
        })
      }
    }

    function endDateBeforeRender ($view, $dates) {
      if ($scope.starttime) {
        var activeDate = moment($scope.starttime).subtract(1, $view).add(1, 'minute');

        $dates.filter(function (date) {
          return date.localDateValue() <= activeDate.valueOf()
        }).forEach(function (date) {
          date.selectable = false;
        })
      }
    }

}]);